package GUI;

import Parser.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;

public class Main implements ActionListener
{
  //attributen
  public JFrame mainframe = new JFrame();
  public Container cp = new Container();
  public JPanel pnlTextkb = new JPanel(),
      pnlExplanation = new JPanel(),
      pnlButton = new JPanel();
  public Steering pnlSteering;
  public Tank pnlTank;
  private JScrollPane jspKB = new JScrollPane(),
      jspAction = new JScrollPane();
  private JTextArea txtKB = new JTextArea(38, 24),
      txtExplanation = new JTextArea(38, 57);
  private JButton btnClose = new JButton("Close"),
      btnSteering = new JButton("Steering"),
      btnTank = new JButton("Tank"),
      btnSteeringAction = new JButton("Pause/Resume"),
      btnMainScreenSteering = new JButton("Main menu"),
      btnMainScreenTank = new JButton("Main menu"),
      btnTankOutFluxIncrease = new JButton("Increase outflux"),
      btnTankOutFluxDecrease = new JButton("Decrease outflux");
  private KBParser parser;
  private Vector vecUniverses, vecVariables, vecRules;

  //constructor
  public Main ()
  {
    mainframe.addWindowListener(new WindowAdapter()
    { //windowlistener om af te sluiten met het kruisje
      public void windowClosing (WindowEvent e)
      {
        System.exit(0);
      }
    });

    //buttons
    btnClose.addActionListener(this);
    btnClose.setSize(new Dimension(100, 50));
    btnSteeringAction.addActionListener(this);
    btnSteeringAction.setSize(new Dimension(150, 50));
    btnSteering.addActionListener(this);
    btnSteering.setSize(new Dimension(150, 50));
    btnTank.addActionListener(this);
    btnTank.setSize(new Dimension(100, 50));
    btnMainScreenSteering.addActionListener(this);
    btnMainScreenSteering.setSize(new Dimension(150, 50));
    btnMainScreenTank.addActionListener(this);
    btnMainScreenTank.setSize(new Dimension(150, 50));
    btnTankOutFluxIncrease.addActionListener(this);
    btnTankOutFluxIncrease.setSize(new Dimension(200, 50));
    btnTankOutFluxDecrease.addActionListener(this);
    btnTankOutFluxDecrease.setSize(new Dimension(200, 50));

    //scrollpanes
    jspKB.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    jspKB.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    jspKB.setAutoscrolls(true);
    jspKB.getViewport().add(txtKB, null);
    jspAction.setHorizontalScrollBarPolicy(JScrollPane.
                                           HORIZONTAL_SCROLLBAR_ALWAYS);
    jspAction.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    jspAction.getViewport().add(txtExplanation, null);

    //panels
    pnlTextkb.add(jspKB);
    pnlTextkb.setSize(new Dimension(250, 650));
    pnlTextkb.setBounds(new Rectangle(0, 0, 250, 650));
    pnlExplanation.add(jspAction);
    pnlExplanation.setSize(new Dimension(700, 650));
    pnlExplanation.setBounds(new Rectangle(250, 0, 700, 650));
    pnlButton.add(btnTank);
    pnlButton.add(btnSteering);
    pnlButton.add(btnClose);
    pnlButton.setSize(new Dimension(850, 50));

    //vullen van txtExplanation
    setMainScreenText();

    //content pane
    cp = mainframe.getContentPane();
    cp.setLayout(new BorderLayout());
    cp.add(pnlTextkb, "West");
    cp.add(pnlExplanation, "Center");
    cp.add(pnlButton, "South");

    //frame
    mainframe.setTitle("AI Blok 4 2006/2007");
    mainframe.setSize(new Dimension(950, 700));
    mainframe.setVisible(true);
    mainframe.setResizable(false);
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

  //methods
  public static void main(String Args[])
  {
    new Main();
  }

  public void setMainScreenText()
  {
    txtExplanation.append ("\n\n\n\n\tWERKING PROGRAMMA\n");
    txtExplanation.append ("\t=======================================================\n\n");
    txtExplanation.append ("\tDruk op de knop sluiten om het progamma af te sluiten.\n");
    txtExplanation.append ("\tKruisje kan ook.\n");
    txtExplanation.append ("\n\n\tTANK\n");
    txtExplanation.append ("\t=======================================================\n\n");
    txtExplanation.append ("\tDruk op de knop gelabeld \'TANK\' en de Knowledge Base\n");
    txtExplanation.append ("\thiervoor wordt automatisch geladen.\n");
    txtExplanation.append ("\tMits hij in de juiste directory staat.\n");
    txtExplanation.append ("\n\n\tAUTO\n");
    txtExplanation.append ("\t=======================================================\n\n");
    txtExplanation.append ("\tDruk op de knop gelabeld \'STEERING\' en de Knowledge Base\n");
    txtExplanation.append ("\thiervoor wordt automatisch geladen.\n");
    txtExplanation.append ("\tMits hij in de juiste directory staat.\n");
    txtExplanation.append ("\n\n\t=======================================================\n");
    txtExplanation.append ("\tDirectory voor de Knowledge Base\n");
    txtExplanation.append ("\t=======================================================\n\n");
    txtExplanation.append ("\tDeze moeten staan in het dezelfde map als de code.\n\n");
    txtExplanation.setEditable(false);

    txtKB.setText("");
    txtKB.setEditable(false);
  }

  public void setKB(Vector vecInhoud)
  {
    for(int a = 0; a < vecInhoud.size(); a++)
    {
      txtKB.append(vecInhoud.elementAt(a).toString() + "\n");
      //JOptionPane.showMessageDialog(null, vecInhoud.elementAt(a).toString(), "PUSHED", 1);
    }
  }

  public void setKB(String str)
  {
  	txtKB.append(str);
  }

  public void actionPerformed (ActionEvent e)
  {
    //close button
    if (e.getSource() == btnClose)
    {
      //JOptionPane.showMessageDialog(null, "message", "title", 1);
      System.exit(0);
    }

    //steering button
    if (e.getSource() == btnSteering)
    {
      //JOptionPane.showMessageDialog(null, "btnSteering", "PUSHED", 1);
      parser = new KBParser("KB_Car.txt");
      vecUniverses = parser.GetUniverses();
      vecVariables = parser.GetVariables();
      vecRules = parser.GetRules();
      setKB(vecUniverses);
      setKB(vecVariables);
      setKB(vecRules);
      pnlExplanation.setVisible(false);
      pnlSteering = new Steering(parser);
      pnlButton.removeAll();
      pnlButton.add(btnSteeringAction);
      pnlButton.add(btnMainScreenSteering);
      cp.add(pnlSteering, "Center");
      pnlSteering.setPreferredSize(new Dimension(700, 700));
      pnlSteering.setDriving(true);
      mainframe.repaint();
    }

    //Tank button
    if (e.getSource() == btnTank)
    {
      //JOptionPane.showMessageDialog(null, "Tank", "PUSHED", 2);
      parser = new KBParser("KB_Tank.txt");
      vecUniverses = parser.GetUniverses();
      vecVariables = parser.GetVariables();
      vecRules = parser.GetRules();
      setKB(vecUniverses);
      setKB(vecVariables);
      setKB(vecRules);
      pnlExplanation.setVisible(false);
      pnlTank = new Tank(parser);
      pnlButton.removeAll();
      pnlButton.add(btnTankOutFluxDecrease);
      pnlButton.add(btnTankOutFluxIncrease);
      pnlButton.add(btnMainScreenTank);
      cp.add(pnlTank, "Center");
      pnlTank.setPreferredSize(new Dimension(700, 700));

      mainframe.repaint();
    }

    //steering action button
    if (e.getSource() == btnSteeringAction)
    {
      if (pnlSteering.getDriving())
      {
        pnlSteering.setDriving(false);
      }
      else if (!pnlSteering.getDriving())
      {
        pnlSteering.setDriving(true);
        pnlSteering.repaint();
      }
    }

    //mainscreen button steering
    if (e.getSource() == btnMainScreenSteering)
    {
      //JOptionPane.showMessageDialog(null, "btnMainscreen", "PUSHED", 2);
      txtKB.setText("");
      pnlExplanation.setVisible(true);
      pnlButton.removeAll();
      pnlButton.add(btnTank);
      pnlButton.add(btnSteering);
      pnlButton.add(btnClose);
      cp.remove(pnlSteering);
      pnlSteering.setDriving(false);
      mainframe.repaint();
    }

    //decrease outflux button
    if (e.getSource() == btnTankOutFluxDecrease)
    {
      if( pnlTank.getVariable("outflux") - 1.0 >= 0 )
          pnlTank.setVariable("outflux", pnlTank.getVariable("outflux") - 1.0);

      pnlTank.repaint();
    }

    //increase outflux button
    if (e.getSource() == btnTankOutFluxIncrease)
    {
      if( pnlTank.getVariable("outflux") + 1.0 <= 16 )
          pnlTank.setVariable("outflux", pnlTank.getVariable("outflux") + 1.0);
      pnlTank.repaint();
    }

    //mainscreen button tank
    if (e.getSource() == btnMainScreenTank)
    {
      //JOptionPane.showMessageDialog(null, "btnMainscreen", "PUSHED", 2);
      txtKB.setText("");
      pnlExplanation.setVisible(true);
      pnlButton.removeAll();
      pnlButton.add(btnTank);
      pnlButton.add(btnSteering);
      pnlButton.add(btnClose);
      cp.remove(pnlTank);
      mainframe.repaint();
    }

  }

    private void jbInit() throws Exception {
    }
}
